    //
//  CategoryViewController.m
//  SpendFunk
//
//  Created by dongmh on 5/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CategoryViewController.h"
#import "image.h"
#import "SpendFunkAppDelegate.h"
#import "Post1ViewController.h"
#import "Global.h"

@implementation CategoryViewController

- (id)init{
	return self;
}
-(void)loadView
{
	[super loadView];
	[self initView];
}
-(void)initView
{	
	
	NSString *path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	UIImage *img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 -20, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[self.view addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	
	//back button
	[path release];
	//[img release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, 5 -20, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onBackClick) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	
	
	// Background for it
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_CATE];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45 -20, img.size.width, img.size.height)];
	[imgvBkgr setImage:img];
	[self.view addSubview:imgvBkgr];
	[imgvBkgr setUserInteractionEnabled:YES];
	[imgvBkgr release];	
	
	// Table for category
	
	m_pTblCate = [[UITableView alloc] initWithFrame:CGRectMake(10, 100-20, 300, 250) style:UITableViewStyleGrouped];
	[m_pTblCate setBackgroundColor:[UIColor clearColor]];
	m_pTblCate.scrollEnabled = YES; 
	[self.view addSubview:m_pTblCate];
	m_pTblCate.delegate = self;
	m_pTblCate.dataSource = self;
//
	//[imgvBkgr addSubview:m_pTblCate];
	
}
#pragma mark -
#pragma mark UITableViewDelegate
BOOL rowChecked = NO; // 
UITableViewCell	*cellChecked;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	if (!rowChecked) {
		[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
		cellChecked = [tableView cellForRowAtIndexPath:[tableView indexPathForCell:cell]];
		rowChecked = YES;
	} 
	
	else {
		[cellChecked setAccessoryType:UITableViewCellAccessoryNone];
		[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
		cellChecked = [tableView cellForRowAtIndexPath:[tableView indexPathForCell:cell]];
	}
	NSString *cateID = [[m_pArrCate objectAtIndex:indexPath.row] valueForKey:kCateID];
	if (!SpendInfo) {
		SpendInfo  = [[NSMutableDictionary alloc] init];
		[SpendInfo retain];
	}
	[SpendInfo setObject:cateID forKey:kCateID];
	//[pDicPost1Infor setObject:[m_pArrCate objectAtIndex:indexPath.row]  forKey:@"cate"];
}
- (void)setArrCate:(NSArray*)arrCate{
	if (m_pArrCate) {
		[m_pArrCate release];
		m_pArrCate = nil;
	}
	m_pArrCate = [arrCate retain];
	[m_pTblCate reloadData];
}

#pragma mark UITableViewDataSource
//int countCate = 5;// = [m_pArrCateItem count];
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
	if (m_pArrCate) {
		return [m_pArrCate count];
	}
	else{
		return 0;
	}
	
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
	}
	//
	cell.textLabel.text = [[m_pArrCate objectAtIndex:indexPath.row] valueForKey:kCateName];
	[cell setAccessoryType:UITableViewCellAccessoryNone];
	return cell;
}
#pragma mark -
#pragma mark Back and Done button touched
- (void)onBackClick{
	[g_AppDelegate backToPost1];
}
- (void)onDoneClick{
	
}
#pragma mark -
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
